package com.hntx.joymusic.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;

import java.io.File;

public class InstallUtils {


    public static void localInstall(Context context, String path) {
        try {
            File file = new File(path);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            int flags = Intent.FLAG_GRANT_READ_URI_PERMISSION;
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            Uri uri = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileProvider", file);//7.0以后 就要使用fileprovider来封装Uri
            } else {
                uri = Uri.fromFile(file);//7.0以前版本可以用这种方式 获取uri
            }
            intent.setFlags(flags);
            intent.setDataAndType(uri, "application/vnd.android.package-archive");
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}